package com.example.product.controller;

import java.util.*;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.product.entity.PmsCategoryEntity;
import com.example.product.service.PmsCategoryService;
import com.example.common.common.utils.*;

import java.util.stream.Collectors;
/**
 * 商品三级分类
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-05-31 12:41:14
 */
@RestController
@RequestMapping("product/pmscategory")
public class PmsCategoryController {
    @Autowired
    private PmsCategoryService pmsCategoryService;


    @Autowired
    BaseMapper baseMapper;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:pmscategory:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = pmsCategoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    @RequestMapping("/listCate")
    // @RequiresPermissions("product:pmscategory:list")
    public List<PmsCategoryEntity> list() {
       List<PmsCategoryEntity> list = pmsCategoryService.get();


        List<PmsCategoryEntity> collect = list.stream().filter(x ->

               x.getParentCid() == 0L
        ).map(x->{
           x.setPmsCategoryEntityList(getChildren(x,list));
            return x;
        }  ).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());


        return collect;


   /*     //1、查出所有分类
        List<PmsCategoryEntity> entities = baseMapper.selectList(null);

        //2、组装成父子的树形结构

        //2.1）、找到所有的一级分类
        List<PmsCategoryEntity> level1Menus = entities.stream().filter(categoryEntity ->
                categoryEntity.getParentCid() == 0
        ).map((menu)->{
            menu.setPmsCategoryEntityList(getChildrens(menu,entities));
            return menu;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return level1Menus;*/

    }

    //递归查找所有菜单的子菜单
    private List<PmsCategoryEntity> getChildrens(PmsCategoryEntity root,List<PmsCategoryEntity> all){

        List<PmsCategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map(categoryEntity -> {
            //1、找到子菜单
            categoryEntity.setPmsCategoryEntityList(getChildrens(categoryEntity,all));
            return categoryEntity;
        }).sorted((menu1,menu2)->{
            //2、菜单的排序
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());

        return children;
    }




    //递归查找所有的子菜单

    public List<PmsCategoryEntity> getChildren(PmsCategoryEntity root,
                                               List<PmsCategoryEntity> all){

        List<PmsCategoryEntity> collect = all.stream().filter(x -> {
            return x.getParentCid() == root.getCatId();
        }).map(x -> {
            root.setPmsCategoryEntityList(getChildren(x,
                    all));
            return x;
        }).sorted((x, y) -> {
            int a= x.getSort()==null?0:x.getSort();

            int b= x.getSort()==null?0:x.getSort();

            return a-b;
        }).collect(Collectors.toList());


        return  collect;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //  @RequiresPermissions("product:pmscategory:info")
    public R info(@PathVariable("catId") Long catId) {
        PmsCategoryEntity pmsCategory = pmsCategoryService.getById(catId);

        return R.ok().put("pmsCategory", pmsCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:pmscategory:save")
    public R save(@RequestBody PmsCategoryEntity pmsCategory) {
        pmsCategoryService.save(pmsCategory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:pmscategory:update")
    public R update(@RequestBody PmsCategoryEntity pmsCategory) {
        pmsCategoryService.updateById(pmsCategory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //  @RequiresPermissions("product:pmscategory:delete")
    public R delete(@RequestBody Long[] catIds) {
        pmsCategoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
